package com.lt.flashcard.view;

import java.util.List;

import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.Promotion;
import com.lt.flashcard.model.service.EcommerceService;
import com.lt.flashcard.model.service.Repository;

/**
 * Manage promotion
 * @author ocsen
 *
 */
public class AdminPromotionVm {
	
	private List<Promotion> promotions = null;
	private Promotion promotion = null;

	@Init
	public void init() {
		Utility.adminLoginOrAccessDenied(false);
		loadData();
	}
	
	/**
	 * Load all current promotion list
	 */
	public void loadData() {
		EcommerceService srv = Repository.getInstance().getEcommerceService();
		promotions = srv.getAllPromotions();
	}
	
	@NotifyChange("promotion")
	@Command
	public void newpromotion() {
		promotion = new Promotion();
	}
	
	@NotifyChange("cancel")
	@Command
	public void cancel() {
		promotion = null;
	}
	
	@Command
	public void save() {
		if(promotion != null) {
			EcommerceService srv = Repository.getInstance().getEcommerceService();
			srv.savePromotion(promotion);
			promotion = null;
			
			loadData();
			BindUtils.postNotifyChange(null, null, this, "promotion");
			BindUtils.postNotifyChange(null, null, this, "promotions");
		}
	}
	
	@NotifyChange("promotions")
	@Command
	public void delete(@BindingParam("id") String id) {
		EcommerceService srv = Repository.getInstance().getEcommerceService();
		srv.deletePromotion(id);
		loadData();
	}

	public List<Promotion> getPromotions() {
		return promotions;
	}

	public void setPromotions(List<Promotion> promotions) {
		this.promotions = promotions;
	}

	public Promotion getPromotion() {
		return promotion;
	}

	public void setPromotion(Promotion promotion) {
		this.promotion = promotion;
	}
}
